/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaMaterial.java
 *
 * Created on 18/06/2011, 16:58:30
 */
package spoc.interfaces;

import javax.swing.JOptionPane;
import spoc.classes.BancoDeDados;
import spoc.classes.DocumentoMascara;
import spoc.classes.Material;

/**
 *
 * @author dolalima
 */
public class JanelaMaterial extends Janela {
    
    private TipoJanela tipo;
    private Material material;

    public JanelaMaterial(TipoJanela tipo, Material m) {
        this.tipo = tipo;
        initComponents();
        this.setTitle("Cadastro de Material");
        
        if (m == null){
            material = new Material();
        }else{
            material = m;
        }
        
        this.setLocationRelativeTo(this.getContentPane());
        
        if(this.tipo == TipoJanela.ALTERACAO){
            botaoCadastrar.setText("Alterar");
            preencher();
        } else if (this.tipo == TipoJanela.INFORMACAO){
              botaoCadastrar.setVisible(false);
              botaoCancelar.setVisible(false);
              desabilitarCampos();
              preencher();
        }
        
        this.campoCodigo.setDocument(new DocumentoMascara(9));
        this.campoNome.setDocument(new DocumentoMascara(80));
        this.campoPreco.setDocument(new DocumentoMascara(8));
        this.campoInfo.setDocument(new DocumentoMascara(400));
        
        reiniciar();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        labelCodigo = new javax.swing.JLabel();
        labelNome = new javax.swing.JLabel();
        labelInformacao = new javax.swing.JLabel();
        labelPreco = new javax.swing.JLabel();
        campoCodigo = new javax.swing.JTextField();
        campoNome = new javax.swing.JTextField();
        campoPreco = new javax.swing.JTextField();
        scrollInformacoes = new javax.swing.JScrollPane();
        campoInfo = new javax.swing.JTextArea();
        botaoCancelar = new javax.swing.JButton();
        botaoCadastrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        labelCodigo.setFont(new java.awt.Font("Ubuntu", 1, 12));
        labelCodigo.setText("Codigo");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        getContentPane().add(labelCodigo, gridBagConstraints);

        labelNome.setFont(new java.awt.Font("Ubuntu", 1, 12));
        labelNome.setText("Nome");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        getContentPane().add(labelNome, gridBagConstraints);

        labelInformacao.setFont(new java.awt.Font("Ubuntu", 1, 12));
        labelInformacao.setText("Informações");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        getContentPane().add(labelInformacao, gridBagConstraints);

        labelPreco.setFont(new java.awt.Font("Ubuntu", 1, 12));
        labelPreco.setText("Preço");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        getContentPane().add(labelPreco, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        getContentPane().add(campoCodigo, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 300;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        getContentPane().add(campoNome, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        getContentPane().add(campoPreco, gridBagConstraints);

        campoInfo.setColumns(20);
        campoInfo.setLineWrap(true);
        campoInfo.setRows(5);
        scrollInformacoes.setViewportView(campoInfo);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 30;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        getContentPane().add(scrollInformacoes, gridBagConstraints);

        botaoCancelar.setText("Cancelar");
        botaoCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelar(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        getContentPane().add(botaoCancelar, gridBagConstraints);

        botaoCadastrar.setText("Cadastrar");
        botaoCadastrar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cadastrar(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        getContentPane().add(botaoCadastrar, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cadastrar(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cadastrar
    
        if(!existemDependencias()){
            material = gerarMaterial();
 
            if(tipo == TipoJanela.CADASTRO) {
                cadastrar();
            } else if(tipo == TipoJanela.ALTERACAO) {
                alterar();
            }
        }
    }//GEN-LAST:event_cadastrar

    private void cancelar(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelar
        // TODO add your handling code here:
        cancelar();
    }//GEN-LAST:event_cancelar

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JanelaMaterial(TipoJanela.CADASTRO,null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoCadastrar;
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JTextField campoCodigo;
    private javax.swing.JTextArea campoInfo;
    private javax.swing.JTextField campoNome;
    private javax.swing.JTextField campoPreco;
    private javax.swing.JLabel labelCodigo;
    private javax.swing.JLabel labelInformacao;
    private javax.swing.JLabel labelNome;
    private javax.swing.JLabel labelPreco;
    private javax.swing.JScrollPane scrollInformacoes;
    // End of variables declaration//GEN-END:variables

    @Override
    protected final void reiniciar() {
        this.campoCodigo.setText("");
        this.campoNome.setText("");
        this.campoPreco.setText("");
        this.campoInfo.setText("");
    }

    @Override
    protected final void preencher() {
        if (material.getCodigo() != 0){
            this.campoCodigo.setText(String.valueOf(material.getCodigo()));            
        }
        this.campoNome.setText(material.getNome());
        this.campoPreco.setText(String.valueOf(material.getPreco()));
        this.campoInfo.setText(material.getInformacao());
    }

    @Override
    protected final boolean existemDependencias() {
        String msg = "Você esqueceu de preencher os \nseguintes campos obrigatórios:\n\n";
        
        boolean existeDependencias = false;
        
        if(campoNome.getText().equals("")){
            msg += "- NOME\n";
            existeDependencias = true;
        }
        if(campoPreco.getText().equals("")){
            msg += "- Preço\n";
            existeDependencias = true;
        }
        
        if(existeDependencias){
            JOptionPane.showMessageDialog(this.getContentPane(), msg);
        }
        
        return existeDependencias;
    }

    @Override
    protected final void desabilitarCampos() {
        this.campoCodigo.setEditable(false);
        this.campoNome.setEditable(false);
        this.campoPreco.setEditable(false);
        this.campoInfo.setEditable(false);
        
    }

    @Override
    protected void cadastrar() {
        if(BancoDeDados.cadastrar(material)){
            int resp = JOptionPane.showConfirmDialog(this, "Produto cadastrado com sucesso. Deseja realizar novo cadastro?", "Concluído", JOptionPane.YES_NO_OPTION);

            this.reiniciar();
            if(resp == JOptionPane.NO_OPTION){
                this.dispose();
            }
        } else {
            JOptionPane.showMessageDialog(this.getContentPane(), "Falha ao cadastrar produto!");
        }
    }

    @Override
    protected void alterar() {
        if(BancoDeDados.alterar(material)){
            JOptionPane.showMessageDialog(this.getContentPane(), "Produto alterado com sucesso!");
            reiniciar();
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this.getContentPane(), "Falha ao alterar produto!");
        }
    }

    private Material gerarMaterial() {
        int codigo = 0;

        if(!campoCodigo.getText().equals("")) {
            codigo = Integer.valueOf(campoCodigo.getText());
        }

        String nome = campoNome.getText();
        double preco = Double.valueOf(campoPreco.getText());
        String informacoes = campoInfo.getText();

        return new Material(codigo, nome, preco, informacoes);
    }
}
